package com.openbravo.pos.payment;

import com.openbravo.pos.util.LuhnAlgorithm;
import com.openbravo.pos.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

public class MagCardParserGeneric
  implements MagCardParser
{
  private String m_sHolderName;
  private String m_sCardNumber;
  private String m_sExpirationDate;
  private StringBuffer track1;
  private StringBuffer track2;
  private StringBuffer track3;
  private static final int READING_STARTSENTINEL1 = 0;
  private static final int READING_STARTSENTINEL2 = 1;
  private static final int READING_STARTSENTINEL3 = 2;
  private static final int READING_CARDTYPE = 3;
  private static final int READING_TRACK1 = 4;
  private static final int READING_TRACK2 = 5;
  private static final int READING_TRACK3 = 6;
  private static final int READING_END = 7;
  private int m_iAutomState;
  private List m_aTrack1;
  private List m_aTrack2;
  private List m_aTrack3;
  private StringBuffer m_sField;
  private char m_cCardType;

  public MagCardParserGeneric()
  {
    reset();
  }

  public void reset()
  {
    this.m_aTrack1 = null;
    this.m_aTrack2 = null;
    this.m_aTrack3 = null;
    this.m_sField = null;
    this.m_cCardType = ' ';
    this.m_sHolderName = null;
    this.m_sCardNumber = null;
    this.m_sExpirationDate = null;
    this.m_iAutomState = 0;
  }

  public void append(char paramChar)
  {
    if (paramChar == '%')
    {
      this.track1 = new StringBuffer();
      this.track2 = new StringBuffer();
      this.track3 = new StringBuffer();
      this.m_aTrack1 = new ArrayList();
      this.m_aTrack2 = null;
      this.m_aTrack3 = null;
      this.m_sField = new StringBuffer();
      this.m_cCardType = ' ';
      this.m_sHolderName = null;
      this.m_sCardNumber = null;
      this.m_sExpirationDate = null;
      this.m_iAutomState = 3;
    }
    else if (this.m_iAutomState == 3)
    {
      this.m_cCardType = paramChar;
      this.m_iAutomState = 4;
    }
    else if ((paramChar == ';') && (this.m_iAutomState == 1))
    {
      this.m_aTrack2 = new ArrayList();
      this.m_sField = new StringBuffer();
      this.m_iAutomState = 5;
    }
    else if ((paramChar == ';') && (this.m_iAutomState == 2))
    {
      this.m_aTrack3 = new ArrayList();
      this.m_sField = new StringBuffer();
      this.m_iAutomState = 6;
    }
    else if ((paramChar == '^') && (this.m_iAutomState == 4))
    {
      this.m_aTrack1.add(this.m_sField.toString());
      this.m_sField = new StringBuffer();
    }
    else if ((paramChar == '=') && (this.m_iAutomState == 5))
    {
      this.m_aTrack2.add(this.m_sField.toString());
      this.m_sField = new StringBuffer();
    }
    else if ((paramChar == '=') && (this.m_iAutomState == 6))
    {
      this.m_aTrack3.add(this.m_sField.toString());
      this.m_sField = new StringBuffer();
    }
    else if ((paramChar == '?') && (this.m_iAutomState == 4))
    {
      this.m_aTrack1.add(this.m_sField.toString());
      this.m_sField = null;
      this.m_iAutomState = 1;
    }
    else if ((paramChar == '?') && (this.m_iAutomState == 5))
    {
      this.m_aTrack2.add(this.m_sField.toString());
      this.m_sField = null;
      this.m_iAutomState = 2;
      checkTracks();
    }
    else if ((paramChar == '?') && (this.m_iAutomState == 6))
    {
      this.m_aTrack3.add(this.m_sField.toString());
      this.m_sField = null;
      this.m_iAutomState = 7;
    }
    else if ((this.m_iAutomState == 4) || (this.m_iAutomState == 5) || (this.m_iAutomState == 6))
    {
      this.m_sField.append(paramChar);
    }
    if ((this.m_iAutomState == 3) || (this.m_iAutomState == 4) || (this.m_iAutomState == 1))
      this.track1.append(paramChar);
    else if ((this.m_iAutomState == 5) || (this.m_iAutomState == 2))
      this.track2.append(paramChar);
    else if ((this.m_iAutomState == 6) || (this.m_iAutomState == 7))
      this.track3.append(paramChar);
  }

  private void checkTracks()
  {
    if (this.m_cCardType != 'B')
      return;
    String str1 = (this.m_aTrack1 == null) || (this.m_aTrack1.size() < 1) ? null : (String)this.m_aTrack1.get(0);
    String str2 = (this.m_aTrack2 == null) || (this.m_aTrack2.size() < 1) ? null : (String)this.m_aTrack2.get(0);
    String str3 = (this.m_aTrack1 == null) || (this.m_aTrack1.size() < 2) ? null : (String)this.m_aTrack1.get(1);
    String str4 = (this.m_aTrack1 == null) || (this.m_aTrack1.size() < 3) ? null : ((String)this.m_aTrack1.get(2)).substring(0, 4);
    String str5 = (this.m_aTrack2 == null) || (this.m_aTrack2.size() < 2) ? null : ((String)this.m_aTrack2.get(1)).substring(0, 4);
    if ((!checkCardNumber(str1)) || ((str2 != null) && (!str1.equals(str2))))
      return;
    if (str3 == null)
      return;
    if (((str4 != null) || (!checkExpDate(str5))) && ((!checkExpDate(str4)) || (!str4.equals(str5))))
      return;
    this.m_sCardNumber = str1;
    this.m_sHolderName = formatHolderName(str3);
    String str6 = str4 == null ? str5 : str4;
    this.m_sExpirationDate = (str6.substring(2, 4) + str6.substring(0, 2));
  }

  private boolean checkCardNumber(String paramString)
  {
    return LuhnAlgorithm.checkCC(paramString);
  }

  private boolean checkExpDate(String paramString)
  {
    return (paramString.length() == 4) && (StringUtils.isNumber(paramString.trim()));
  }

  private String formatHolderName(String paramString)
  {
    int i = paramString.indexOf(47);
    if (i >= 0)
      return paramString.substring(i + 1).trim() + ' ' + paramString.substring(0, i);
    return paramString.trim();
  }

  public boolean isComplete()
  {
    return this.m_sCardNumber != null;
  }

  public String getHolderName()
  {
    return this.m_sHolderName;
  }

  public String getCardNumber()
  {
    return this.m_sCardNumber;
  }

  public String getExpirationDate()
  {
    return this.m_sExpirationDate;
  }

  public String getTrack1()
  {
    return this.track1 == null ? null : this.track1.toString();
  }

  public String getTrack2()
  {
    return this.track2 == null ? null : this.track2.toString();
  }

  public String getTrack3()
  {
    return this.track3 == null ? null : this.track3.toString();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.payment.MagCardParserGeneric
 * JD-Core Version:    0.6.0
 */